home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14116 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  62 lines

  1. Path: tank.news.pipex.net!pipex!iol!usenet
  2. From: David Byrden <Goyra@iol.ie>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: stl newbie help
  5. Date: 28 Mar 1996 21:18:58 GMT
  6. Organization: Ireland On-Line
  7. Message-ID: <4jevo2$vae@nuacht.iol.ie>
  8. References: <315ae9a1.455438@news.en.com>
  9. NNTP-Posting-Host: dialup-193.dublin.iol.ie
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22KIT (Windows; I; 16bit)
  14.  
  15. dpsm@en.com (Dale P. Smith) wrote:
  16.  
  17. >
  18. >What I want to understand how to do is declare a function class that
  19. >can be used with something like for_each:
  20. >    for_each(Table.begin(), Table.end(), FunctionClassForEachThing);
  21. >
  22. >I know that I have to create a class that has operator() in it. What
  23. >should this thing look like?  I can't find (or can't see) any examples
  24. >in the stl documentation.
  25. >
  26.  
  27.  
  28.    Dale;
  29.  
  30.   The various algorithms require different functor signatures; void 
  31. return, bool return, value return, one parameter, 2 parameters etc. In 
  32. your example, this signature is required;
  33.  
  34.  
  35.      void Function( String& ) ;
  36.  
  37. So, we could use an ordinary function, like the one above;
  38.  
  39.      void AppendAnE( String& ) ;
  40.  
  41.      for_each(Table.begin(), Table.end(), AppendAnE );
  42.  
  43.  
  44. Or, we could do this;
  45.  
  46. struct AppendA
  47. {
  48.    char letter ;
  49.    AppendA( const char& c ) : letter( c ) {}
  50.    void operator()( String& ) ;   // code it yourself
  51. } ;
  52.  
  53.  
  54.      for_each(Table.begin(), Table.end(), AppendAn( 'e' ) );
  55.  
  56.  
  57. See?
  58.  
  59.                                           David
  60.  
  61.  
  62.